<!DOCTYPE html>
<html class="iframe-h" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>网站后台管理模版</title>
		<link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}"/>
		<link rel="stylesheet" type="text/css" th:href="@{/css/admin.css}"/>
		<link th:href="@{/lib/bootstraps/css/bootstrap.css}" rel="stylesheet" />
	</head>

	<body>
		<div class="wrap-container clearfix">
				<div class="column-content-detail">
					<form class="layui-form" action="">
						<div class="layui-form-item">
							<div class="layui-inline tool-btn">
								<button class="layui-btn layui-btn-small layui-btn-normal addText" data-url="/toAddTask"><i class="layui-icon">&#xe654;</i></button>
								<button class="layui-btn layui-btn-small layui-btn-danger remove"  data-url="/removeTaskData"><i class="layui-icon">&#xe640;</i></button>
								<button class="layui-btn layui-btn-small ayui-btn-normal modify"  data-url="/toModifyTextData"><i class="layui-icon">&#xe614;</i></button>
							</div>
							<!--<div class="layui-inline">
								<input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
							</div>-->

							<!--<button class="layui-btn layui-btn-normal" lay-submit="search">搜索</button>-->
						</div>
					</form>
					<table class="layui-table" lay-data="{height: 'full', cellMinWidth: 80,  limit:15,limits:[15,30],
						url:'/tasks', page:true, id:'test'}" lay-filter="test" id="test">
						<thead>
						<tr>
							<th lay-data="{type:'checkbox'}">ID</th>
							<th lay-data="{field:'jobName',align:'center'}">任务名称</th>
							<th lay-data="{field:'jobGroup', align:'center'}">任务分组</th>
							<th lay-data="{field:'description', align:'center'}">任务描述</th>
							<th lay-data="{field:'jobClassName', align:'center'}">执行类</th>
							<th lay-data="{field:'cronExpression', align:'center'}">执行时间</th>
							<th lay-data="{fixed: 'right', width:300, align:'center', toolbar: '#barDemo'}"></th>
						</tr>
						</thead>
					</table>

					<div class="modal fade" id="modifyTask" tabindex="-1" role="dialog"
						 aria-labelledby="myModalLabelModify" aria-hidden="true">
						<div class="modal-dialog" style="width:50%;margin-top:10%;">
							<div class="modal-content">
								<div class="modal-header">
									<button type="button" class="close"
											data-dismiss="modal" aria-hidden="true">
										&times;
									</button>
									<h4 class="modal-title" id="myModalLabelModify">
										修改定时任务
									</h4>
								</div>

								<form class="layui-form column-content-detail" id="actAdd">
									<div class="layui-tab-item layui-show">
										<div class="layui-form-item">
											<label class="layui-form-label">任务名称：</label>
											<div class="layui-input-block">
												<input type="hidden" id="oldJobName" name="oldJobName"   autocomplete="off" class="layui-input">
												<input type="text" id="jobName" name="jobName"  required lay-verify="required" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
											</div>
										</div>
										<div class="layui-form-item">
											<label class="layui-form-label">任务分组：</label>
											<div class="layui-input-block">
												<input type="hidden" id="oldJobGroup" name="oldJobGroup"  autocomplete="off" class="layui-input">
												<input type="text" id="jobGroup" name="jobGroup"  required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
											</div>
										</div>
										<div class="layui-form-item">
											<label class="layui-form-label">任务描述：</label>
											<div class="layui-input-block">
												<input type="text" id="description" name="description"  required lay-verify="required" placeholder="请输入任务描述" autocomplete="off" class="layui-input">
											</div>
										</div>
										<div class="layui-form-item">
											<label class="layui-form-label">执行类：</label>
											<div class="layui-input-block">
												<input type="text" id="jobClassName" name="jobClassName"  required lay-verify="required" placeholder="请输入类的根路径" autocomplete="off" class="layui-input">
											</div>
										</div>
										<div class="layui-form-item">
											<label class="layui-form-label">执行时间：</label>
											<div class="layui-input-block">
												<input type="text" id="cronExpression" name="cronExpression"  required lay-verify="required" placeholder="请查询cron表达式" autocomplete="off" class="layui-input">
											</div>
										</div>

									</div>
									<div class="layui-form-item" style="padding-left: 10px;">
										<div class="layui-input-block">
											<button class="layui-btn layui-btn-normal" type="submit">立即提交</button>
											<button type="reset" class="layui-btn layui-btn-primary">重置</button>
										</div>
									</div>
								</form>
							</div>
						</div>
					</div>

				</div>
		</div>


		<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/js/common.js}" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" th:src="@{/lib/bootstraps/js/jquery-3.2.1.min.js}"></script>
		<script type="text/javascript" th:src="@{/lib/bootstraps/js/bootstrap.js}"></script>
		<script type="text/javascript" th:src="@{/js/basic/dateutil.js}"></script>
		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-sm" lay-event="execute">执行</a>
			{{#  if(d.triggerState == "ACQUIRED" || d.triggerState == "WAITING"){ }}
				<a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="stop">停止</a>
			{{#  }else if(d.triggerState == "PAUSED"){ }}
				<a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="resume">恢复</a>
			{{#  }}}

		</script>
		<script>

            $(function () {


                $('#modifyTask').on('hide.bs.modal', function () {
                    $('#actAdd')[0].reset();
                });


            });

            layui.config({
                base: '/js/module/'
            }).extend({
                dialog: 'dialog',
            });

			layui.use(['table','tree','jquery','dialog'], function() {
				var table = layui.table,
					form = layui.form,
                    $ = layui.jquery,
					tree = layui.tree,
                    dialog = layui.dialog;

                //获取当前iframe的name值
                var iframeObj = $(window.frameElement).attr('name');

				table.on('tool(test)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'execute'){
                        $.ajax({
                            url:"/triggerTask",
                            type:"post",
                            data:{'time':(new Date()).toString(),"jobName":data.jobName,'jobGroup':data.jobGroup},
                            success: function(data) {
                                if(data.code === 200){
                                    layer.msg("执行成功");
                                }else{
                                    layer.msg(data.msg);
								}
                                table.reload('test',{page:{curr:1}});
                            }
                        });
                    }else if(obj.event === 'stop'){
                        $.ajax({
                            url:"/stopTask",
                            type:"post",
                            data:{'time':(new Date()).toString(),"jobName":data.jobName,'jobGroup':data.jobGroup},
                            success: function(data) {
                                if(data.code === 200){
                                    layer.msg("停止成功");
                                }else{
                                    layer.msg(data.msg);
                                }
                                table.reload('test',{page:{curr:1}});
                            }
                        });
					}else if(obj.event === 'resume'){
                        $.ajax({
                            url:"/resumeTask",
                            type:"post",
                            data:{'time':(new Date()).toString(),"jobName":data.jobName,'jobGroup':data.jobGroup},
                            success: function(data) {
                                if(data.code === 200){
                                    layer.msg("恢复成功");
                                }else{
                                    layer.msg(data.msg);
                                }
                                table.reload('test',{page:{curr:1}});
                            }
                        });
                    }
				});

                $('.addText').click(function() {
                    let url=$(this).attr('data-url');
                    //将iframeObj传递给父级窗口,执行操作完成刷新
                    page("新建任务", url, iframeObj, w = '80%', h = '80%');
                    return false;

                }).mouseenter(function() {

                    dialog.tips('新建任务', '.addText');

                });

                $('.modify').click(function() {
                    var checkStatus = table.checkStatus('test'),data = checkStatus.data;
                    if(data.length<1){
                        layer.msg("请选择一条记录")
                        return false;
                    }
                    if(data.length>1){
                        layer.msg("只能选择一条记录")
                        return false;
                    }
                    if(data[0].triggerState == "ACQUIRED" || data[0].triggerState == "WAITING"){
                        layer.msg("运行中的任务不可修改！");
                        return false;
                    }
                    $('#jobName').val(data[0].jobName);
                    $('#oldJobName').val(data[0].oldJobName);
                    $('#jobGroup').val(data[0].jobGroup);
                    $('#oldJobGroup').val(data[0].oldJobGroup);
                    $('#description').val(data[0].description);
                    $('#jobClassName').val(data[0].jobClassName);
                    $('#cronExpression').val(data[0].cronExpression);
                    $('#modifyTask').modal({
                        keyboard: true
                    });

                    return false;

                }).mouseenter(function() {

                    dialog.tips('设置', '.modify');

                });


                $('.remove').click(function() {
                    var checkStatus = table.checkStatus('test'),data = checkStatus.data;
                    if(data.length<1){
                        layer.msg("请选择一条记录")
                        return false;
                    }
                    if(data.length>1){
                        layer.msg("只能选择一条记录")
                        return false;
                    }
                    let url=$(this).attr('data-url');
                    if(data[0].triggerState == "ACQUIRED" || data[0].triggerState == "WAITING"){
                        layer.msg("运行中的任务不可删除！");
                        return false;
                    }
                    layer.confirm( '您确定要删除选中项',function(index){
                        let dataForm = {
                            time:(new Date()).toString(),
                            jobName:data[0].jobName,
                            jobGroup:data[0].jobGroup
                        };
						$.ajax({
							url:url,
							data:dataForm,
							type: 'post',
							dataType:'json',
							success:function (data){
								if(data.code === 200){
									layer.close(index);
									layer.msg("删除成功");
									table.reload('test',{page:{curr:1}});
								}else{
                                    layer.msg(data.msg);
								}
							}
						});
                    });
                    return false;

                }).mouseenter(function() {
                    dialog.tips('删除', '.remove');
                })
            });


            $("#actAdd").bind("submit",function(){
                let data = {
                    jobName:$('#jobName').val(),
                    jobGroup:$('#jobGroup').val(),
                    description:$('#description').val(),
                    jobClassName: $("#jobClassName").val(),
                    cronExpression: $("#cronExpression").val(),
                    oldJobName: $("#oldJobName").val(),
                	oldJobGroup: $("#oldJobGroup").val()
                }

                $.ajax({
                    url: '/addTask',
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg('修改成功');
                            $("#modifyTask").modal('hide');
                            layui.table.reload('test',{page:{curr:1}});
                        } else {
                            layer.msg('修改失败，请刷新页面');
                        }
                    }
                });
                return false;
            });

		</script>
	</body>

</html>